{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-82-d60b867515b9>, line 19)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-82-d60b867515b9>\"\u001b[0;36m, line \u001b[0;32m19\u001b[0m\n\u001b[0;31m    (/)\u001b[0m\n\u001b[0m     ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import json\n",
    "\n",
    "#Read text file which is HTML\n",
    "#ToDo: take it from the mongoDB\n",
    "file = open('demo_html.txt', 'r') \n",
    "\n",
    "def sanitizeText(str):\n",
    "    return str.lower().replace('\\n', '').replace('\\t', '').strip()\n",
    "\n",
    "def sanitizeKey(str):\n",
    "    return str.lower().replace(' ', '_').strip()\n",
    "\n",
    "\n",
    "soup = BeautifulSoup(file)\n",
    "#Parsing\n",
    "title = sanitizeText(soup.find('div', class_='title').get_text())\n",
    "description = sanitizeText(soup.find(id=\"offerDescription\").get_text())\n",
    "\n",
    "details = soup.find('div', class_='tabularDetails')\n",
    "parts = details.find_all('div')\n",
    "lobbying = {}\n",
    "for element in parts:\n",
    "    lobbying[sanitizeKey(element.label.get_text())] = sanitizeText(element.span.get_text())\n",
    "print json.dumps(lobbying)\n",
    "    \n",
    "#print parts[1].label.get_text()\n",
    "# for element in details:\n",
    "#     print element.label.get_text()\n",
    "\n",
    "\n",
    "result = {}\n",
    "result['title'] = title\n",
    "result['description'] = description\n",
    "json_data = json.dumps(result)\n",
    "#print json_data\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
